package com.example.navigationbasicdemo.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import com.example.navigationbasicdemo.R;

/**
 * @author: njb
 * @date: 2020/9/5 0005 1:20
 * @desc:
 */
public class MainFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        //方法一:未采用safe args的传递方式
        view.findViewById(R.id.btnToSecondFragment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("name", "Michael");
                bundle.putInt("age", 30);
                Navigation.findNavController(v).navigate(R.id.action_mainFragment_to_secondFragment, bundle);
            }
        });
        //方法二://通过safe args完成参数传递
 /*       view.findViewById(R.id.btnToSecondFragment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new MainFragmentArgs.Builder().setName("小霍").setAge(18).build().toBundle();
                Navigation.findNavController(v).navigate(R.id.action_mainFragment_to_secondFragment, bundle);
            }
        });*/
        view.findViewById(R.id.btnToMineFragment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.findNavController(v).navigate(R.id.action_mainFragment_to_mineFragment);
            }
        });
        return view;
    }
}
